package com.das.mechanic_base.widget;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.utils.SpHelper;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by chenSi on 2018/3/19.
 */

public class X3ReAloneWindow implements View.OnClickListener, PopupWindow.OnDismissListener {

    private PopupWindow popupWindow;

    private Activity mContext;
    private long receiveBaseId;
    private long workBaseId;
    private TextView tv_re_alone;
    private TextView tv_cancel;
    private TextView tv_camera;
    private int type;// 0 检测单 1 施工单
    private TextView tv_video;
    private ImageView iv_red;
    private RelativeLayout rl_live;
    private boolean isshowred = false;
    private TextView tv_look;
    private boolean isSdk;

    public X3ReAloneWindow(Activity mContext) {
        this.mContext = mContext;

    }

    public void showPopupWindow(View v,boolean isshowred) {
        this.isshowred = isshowred;
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        //设置PopupWindow的View
        View view = LayoutInflater.from(mContext).inflate(R.layout.x3_re_alone_pop_view, new LinearLayout(mContext),false);
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(null);
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        //popupWindow.setAnimationStyle(R.style.PopupWindow1);
        //设置位置
        popupWindow.showAsDropDown(v);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //设置PopupWindow的View点击事件
        setOnPopupViewClick(view);
        //设置背景色
        setBackgroundAlpha(1.0f);
    }

    private void setOnPopupViewClick(View view) {
        tv_re_alone = view.findViewById(R.id.tv_re_alone);
        tv_re_alone.setOnClickListener(this);
    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = alpha;
        if (alpha != 1) {
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }else {
            mContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        mContext.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_re_alone) {
            if (iPopClick != null) {
                iPopClick.reAlone();
                onDismiss();
            }
        }
    }

    /**
     * 传入施工单状态 未开始 施工中 已经结束
     * @param status
     */
    public void showWorkStatus(long status) {
        if (status > 1) {
            tv_cancel.setVisibility(View.GONE);
        }
    }

    public void providerReceive (long receiveBaseId,long workBaseId,int type) {
        this.receiveBaseId = receiveBaseId;
        this.workBaseId = workBaseId;
        this.type = type;
        MobclickAgent.onPageStart(type == 0? "检测单功能提示弹窗" : "施工单功能提示弹窗");
        if (tv_look != null) {
            tv_look.setVisibility((receiveBaseId == 0) ? View.GONE : View.VISIBLE);
        }
    }

    IPopClick iPopClick;

    public void setiOnPopClick(IPopClick iOnItemCameraClick) {
        this.iPopClick = iOnItemCameraClick;
    }

    public interface IPopClick {
        void onPopDismiss();
        void reAlone();
    }

    @Override
    public void onDismiss() {
        if (iPopClick!=null){
            iPopClick.onPopDismiss();
        }
        MobclickAgent.onPageEnd(type == 0? "检测单功能提示弹窗" : "施工单功能提示弹窗");
        setBackgroundAlpha(1);
        popupWindow.dismiss();

    }
}
